using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace P4Shelf
{
    public partial class Form1 : Form
    {
        private void _OnDataChanged_CreateTab()
        {
            if (!this._ShelfGUI.Connected ||
                this.createtab_name.Text.Length <= 0 ||
                this.createtab_files.CheckedItems.Count <= 0 ||
                this.createtab_desc.Text.Length <= 0)
            {
                this.createtab_activate.Enabled = false;
                return;
            }
            
            this.createtab_activate.Enabled = true;
        }
        private bool _Tab3_IsReady()
        {
            if (!this._ShelfGUI.Connected)
            {
                return true;
            }

            if (this.configtab_password.Text != this.configtab_confirmpassword.Text || 
                this._ShelfGUI.ConfigTab.User.Length < 1 ||
                this._ShelfGUI.ConfigTab.ClientSpec.Length < 1 ||
                this._ShelfGUI.ConfigTab.Depo.Length < 1)
            {
                return false;
            }

            return this._ShelfGUI.ConfigTab.HasChanged;
        }

        private void _Update_Tab3()
        {
            if (this.configtab_apply.Enabled != _Tab3_IsReady())
            {
                this.configtab_apply.Enabled = !this.configtab_apply.Enabled;
            }

        }

        private void _Update_Tab2()
        {
            bool bValid = this._ShelfGUI.ManageTab.Unpack();

            // reset data
            this.managetab_job.Text = this._ShelfGUI.ManageTab.JobName;
            this.managetab_desc.Text = this._ShelfGUI.ManageTab.Comment;

            this.managetab_files.Items.Clear();
            this.managetab_files.Items.AddRange(this._ShelfGUI.ManageTab.Files);
            this.managetab_files.Enabled = false;
            for (int i = 0; i < this.managetab_files.Items.Count; ++i)
            {
                this.managetab_files.SetItemChecked(i, true);
            }

            this.managetab_update.Enabled = bValid;
            this.managetab_delete.Enabled = System.IO.File.Exists(this._ShelfGUI.ManageTab.ShelfLocalPathName);
            this.managetab_unshelve.Enabled = bValid;
            this.managetab_resolve.Enabled = bValid;

            // call global refresh
            _Refresh();
        }
        private void _Refresh()
        {
            this.createtab_files.Items.Clear();
            this.createtab_files.Items.AddRange(_ShelfGUI.CheckedOutFiles());
        }

        private void _Create_ConfigTab()
        {
            this.configtab_password.PasswordChar = '*';
            this.configtab_confirmpassword.PasswordChar = '*';

            this.configtab_connections.Items.AddRange(_ShelfGUI.ConfigTab.P4Connections.ToArray());
            this.configtab_shelfdir.Text = _ShelfGUI.ConfigTab.LocalShelfDir;

            this._Helper.RegisterControl(tabControl1.TabPages[2]);
            this._Helper.RegisterControl(configtab_shelfdir);
            this._Helper.RegisterControl(configtab_password);
            this._Helper.RegisterControl(configtab_confirmpassword);
            this._Helper.RegisterControl(configtab_browse);
            this._Helper.RegisterControl(configtab_new);
            this._Helper.RegisterControl(configtab_del);
            this._Helper.RegisterControl(configtab_apply);
            this._Helper.RegisterControl(configtab_user);
            this._Helper.RegisterControl(configtab_clientSpec);
            this._Helper.RegisterControl(configtab_depo);

            this.configtab_user.Enabled = false;
            this.configtab_clientSpec.Enabled = false;
            this.configtab_depo.Enabled = false;
            this.configtab_password.Enabled = false;
            this.configtab_confirmpassword.Enabled = false;

            _Update_Tab3();
        }

        private void _Create_ManageTab()
        {
            this.managetab_update.Enabled = false;
            this.managetab_delete.Enabled = false;
            this.managetab_unshelve.Enabled = false;
            this.managetab_resolve.Enabled = false;

            this._Helper.RegisterControl(tabControl1.TabPages[1]);
            this._Helper.RegisterControl(managetab_job);
            this._Helper.RegisterControl(managetab_name);
            this._Helper.RegisterControl(managetab_files);
            this._Helper.RegisterControl(managetab_desc);
            this._Helper.RegisterControl(managetab_unshelve);
            this._Helper.RegisterControl(managetab_delete);
            this._Helper.RegisterControl(managetab_update);
        }

        private void _Create_CreateTab()
        {
            this.createtab_files.Items.AddRange(_ShelfGUI.CheckedOutFiles());

            this._Helper.RegisterControl(tabControl1.TabPages[0]);
            this._Helper.RegisterControl(createtab_name);
            this._Helper.RegisterControl(createtab_job);
            this._Helper.RegisterControl(createtab_release);
            this._Helper.RegisterControl(createtab_files);
            this._Helper.RegisterControl(createtab_desc);
            this._Helper.RegisterControl(createtab_activate);
        }
    }
}